package com.ruoyi.hr.service.impl.welfare.welfareRegion;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.hr.domain.welfare.welfareregion.WelfareType;
import com.ruoyi.hr.domain.welfare.welfareregion.WelfareTypeColumns;
import com.ruoyi.hr.mapper.welfare.welfareRegion.WelfareTypeMapper;
import com.ruoyi.hr.service.welfare.welfareRegion.WelfareTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WelfareTypeServiceImpl implements WelfareTypeService {

    @Autowired
    WelfareTypeMapper welfareTypeMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<WelfareType> findWelfareType(WelfareType welfareType, int PageNum, int PageSize) {
        return welfareTypeMapper.findWelfareType(welfareType, (PageNum - 1) * PageSize, PageSize);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<WelfareType> findWelfareTypes() {
        return welfareTypeMapper.findWelfareTypes();
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int findCount(WelfareType welfareType) {
        return welfareTypeMapper.findCount(welfareType);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateIsLock(WelfareType welfareType) {
        return welfareTypeMapper.updateIsLock(welfareType);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int insertWelfareType(WelfareType welfareType) {
        return welfareTypeMapper.insertWelfareType(welfareType);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateWelfareType(WelfareType welfareType) {
        return welfareTypeMapper.updateWelfareType(welfareType);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int deleteWelfareType(String ids) {
        int num = 0;
        if (ids.indexOf(",") != -1) {
            String[] arry = ids.split("\\,");
            for (String id : arry) {
                num += welfareTypeMapper.deleteWelfareType(id);
            }
        } else {
            num = welfareTypeMapper.deleteWelfareType(ids);
        }
        return num;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public WelfareType findWelfareTypeById(int id) {
        return welfareTypeMapper.findWelfareTypeById(id);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<WelfareTypeColumns> findAll() {
        List<WelfareType> welfareTypes = welfareTypeMapper.findAll();
        List<WelfareTypeColumns> columns = new ArrayList<WelfareTypeColumns>();
        WelfareTypeColumns welfareTypeColumns = new WelfareTypeColumns();
        for (WelfareType welfareType : welfareTypes) {

            welfareTypeColumns = new WelfareTypeColumns();
            String typeReplace = welfareType.getTitle().replaceAll(" ", "").replaceAll("-", "").replaceAll("–", "");
            welfareTypeColumns.setField(welfareType.getCode() + "@@基础上限");
            welfareTypeColumns.setTitle(typeReplace + "基础上限");
            columns.add(welfareTypeColumns);

            welfareTypeColumns = new WelfareTypeColumns();
            welfareTypeColumns.setField(welfareType.getCode() + "@@基础下限");
            welfareTypeColumns.setTitle(typeReplace + "基础下限");
            columns.add(welfareTypeColumns);

            welfareTypeColumns = new WelfareTypeColumns();
            welfareTypeColumns.setField(welfareType.getCode() + "@@比例");
            welfareTypeColumns.setTitle(typeReplace + "比例");
            columns.add(welfareTypeColumns);

            welfareTypeColumns = new WelfareTypeColumns();
            welfareTypeColumns.setField(welfareType.getCode() + "@@加项");
            welfareTypeColumns.setTitle(typeReplace + "加项");
            columns.add(welfareTypeColumns);

            welfareTypeColumns = new WelfareTypeColumns();
            welfareTypeColumns.setField(welfareType.getCode() + "@@进位方式id");
            welfareTypeColumns.setTitle(typeReplace + "进位方式");
            columns.add(welfareTypeColumns);
        }
        return columns;
    }
}
